﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BaseClasses;

namespace SVAccountingWebUI
{
    public partial class Person : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (GridView1.SelectedIndex != -1)
            {
                SetFreeCompanies();
            }
        }

        private void SetFreeCompanies()
        {
            if (Membership.GetUser(GridView1.SelectedDataKey["UserName"].ToString()) != null)
            {
                divAccountantCompanies.Visible = true;

                SVAccountingBL.UsersODS dbAccess = new SVAccountingBL.UsersODS();

                ddlFreeCompanies.DataSource = dbAccess.GetFreeCompaniesForAccounant(GridView1.SelectedDataKey["UserName"].ToString());
                ddlFreeCompanies.DataValueField = "CompanyID";
                ddlFreeCompanies.DataTextField = "CompanyName";
                ddlFreeCompanies.DataBind();

                if (ddlFreeCompanies.Items.Count > 0)
                {
                    ddlFreeCompanies.Visible = true;
                    imgbtnAddUserToProject.Visible = true;

                }
                else
                {
                    ddlFreeCompanies.Visible = false;
                    imgbtnAddUserToProject.Visible = false;
                }
            }
            else
            {
                divAccountantCompanies.Visible = false;

                GridView1.DataBind();
                GridView1.SelectedIndex = -1;
                Master.MessageText = "Корисникот кој сакавте да го одберете, во меѓувреме е избришан од страна на друг администратор";
                Master.MessageColor = System.Drawing.Color.Red;
            }
        }

        protected void imgbtnAddUserToProject_Click(object sender, ImageClickEventArgs e)
        {
            SVAccountingBL.SVALinqDataContext db = new SVAccountingBL.SVALinqDataContext();

            //ODSCompaniesAccountant.InsertParameters["userName"].DefaultValue = GridView1.SelectedDataKey["UserName"].ToString();
            //ODSCompaniesAccountant.InsertParameters["companyID"].DefaultValue = ddlFreeCompanies.SelectedValue;
            //int k = ODSCompaniesAccountant.Insert();
            try
            {
                SVAccountingBL.rAccountantPerson theRelation = new SVAccountingBL.rAccountantPerson();
                SVAccountingBL.Person thePerson = (from p in db.Persons
                                    where p.UserName == GridView1.SelectedDataKey["UserName"].ToString()
                                    select p).Single();

                theRelation.Person = thePerson;
                theRelation.PersonID = thePerson.PersonID;
                theRelation.CompanyID = int.Parse(ddlFreeCompanies.SelectedValue);
                db.rAccountantPersons.InsertOnSubmit(theRelation);
                db.SubmitChanges();
            }
            catch (Exception exx)
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
            }
            SetControlsAfterAssgnDeleteCompany();
            ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertSuccess").ToString();
            ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
        }

        protected void ODSCompaniesAccountant_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                SetControlsAfterInsert();
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertSuccess").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "InsertError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
                e.ExceptionHandled = true;
            }
        }

        protected void ODSCompaniesAccountant_Deleted(object sender, ObjectDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "DeleteSuccess").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                ((Label)Master.FindControl("lblMasterMessage")).Text = GetGlobalResourceObject("Resource", "DeleteError").ToString();
                ((Label)Master.FindControl("lblMasterMessage")).ForeColor = System.Drawing.Color.Red;
                e.ExceptionHandled = true;
            }
            SetControlsAfterAssgnDeleteCompany();
        }

        protected void ODSCompaniesAccountant_Deleting(object sender, ObjectDataSourceMethodEventArgs e)
        {
            ((SVAccountingBL.Company)e.InputParameters["cpToRemove"]).rAccountantPersons.Add(new SVAccountingBL.rAccountantPerson() 
            {
                Person = new SVAccountingBL.Person(){ UserName =  GridView1.SelectedDataKey["UserName"].ToString() }
                
            });
            
        }

        private void SetControlsAfterAssgnDeleteCompany()
        {
            SetFreeCompanies();
            GridView2.DataBind();
        }
    }
}

